API Documentation
Public Member Functions | Static Public Member Functions | List of all members
nkMemory::Buffer Class Referencefinal

A buffer holding binary data. More...

Public Member Functions

 Buffer ()
 
 Buffer (unsigned long long size)
 
 Buffer (unsigned char *data, unsigned long long size)
 
 Buffer (const Buffer &other)
 
 Buffer (Buffer &&other)
 
 ~Buffer ()
 
unsigned char * getData () const
 
unsigned long long getSize () const
 
bool empty () const
 
unsigned char & front ()
 
unsigned char & back ()
 
void clear ()
 
void resize (unsigned long long size)
 
unsigned char & append (unsigned char value)
 
unsigned char & append (unsigned char value)
 
BufferInfo relinquishDataOwnership ()
 
unsigned char & operator[] (unsigned long long index)
 
const unsigned char & operator[] (unsigned long long index) const
 
Bufferoperator= (const Buffer &other)
 
Bufferoperator= (Buffer &&other)
 
template<typename T >
 Buffer (const std::vector< T > &vec)
 

Static Public Member Functions

static Buffer createAndTakeMemory (unsigned char *data, unsigned long long size)
 

Detailed Description

A buffer holding binary data.

The Buffer class owns its memory, and is hard coded to handle binary data shaped as unsigned char. It is meant to manage all memory and operations in the DLL itself, meaning that passing one from user space to dll space is safe. This class will be the basis to safely exchange data with components. It avoids having to play with vectors that can be unsafe due to their build time template parameters.

If you need something else than unsigned char, take a look at the BufferCast template class. Its purpose is to use this class, with an API casting for you. For data that doesn't need to be copied, BufferView can be the answer as it is only meant to offer a bridge to the data.

Constructor & Destructor Documentation

◆ Buffer() [1/6]

nkMemory::Buffer::Buffer ( )

Default constructor. The buffer will be empty.

◆ Buffer() [2/6]

nkMemory::Buffer::Buffer ( unsigned long long  size)

Size constructor. Will allocate the size requested within the buffer. Memory will be 0-cleared.

Parameters
sizeThe size of the buffer to create. As this buffer is binary, size is in bytes.

◆ Buffer() [3/6]

nkMemory::Buffer::Buffer ( unsigned char *  data,
unsigned long long  size 
)

Data constructor. Will copy the data into its internal memory.

Parameters
dataPointer to the data to copy.
sizeThe size of the data to copy, in bytes.

◆ Buffer() [4/6]

nkMemory::Buffer::Buffer ( const Buffer other)

Copy constructor. Will duplicate the data.

Parameters
otherThe buffer to copy.

◆ Buffer() [5/6]

nkMemory::Buffer::Buffer ( Buffer &&  other)

Move constructor. Data will be moved into the buffer being constructed.

Parameters
otherThe buffer to move.

◆ ~Buffer()

nkMemory::Buffer::~Buffer ( )

Destructor. The destructor frees the memory, invalidating all potential pointers to it.

◆ Buffer() [6/6]

template<typename T >
nkMemory::Buffer::Buffer ( const std::vector< T > &  vec)

Utility copy constructor with vectors.

Parameters
vecThe vector to copy from.

Member Function Documentation

◆ getData()

unsigned char* nkMemory::Buffer::getData ( ) const
Returns
The pointer over the internal data.

◆ getSize()

unsigned long long nkMemory::Buffer::getSize ( ) const
Returns
The size of the buffer, in bytes.

◆ empty()

bool nkMemory::Buffer::empty ( ) const
Returns
Whether the buffer is empty (true) or not (false). An empty buffer has a size of 0.

◆ front()

unsigned char& nkMemory::Buffer::front ( )
Returns
A reference over the first element of the buffer.

◆ back()

unsigned char& nkMemory::Buffer::back ( )
Returns
A reference over the last element of the buffer.

◆ clear()

void nkMemory::Buffer::clear ( )

Clears the buffer, freeing its internal memory and resetting it to its empty state.

◆ resize()

void nkMemory::Buffer::resize ( unsigned long long  size)

Resizes the buffer for it to fit a given size. This will trigger a reallocation of the data. In the process, the buffer will copy its content to the new memory area.

Parameters
sizeThe size to fit, in bytes.

◆ append() [1/2]

unsigned char& nkMemory::Buffer::append ( unsigned char  value)

Appends a byte to the buffer. This will cause the buffer to reallocate and copy data around. Use it wisely.

Parameters
valueThe value of the byte to append.
Returns
The reference over the element appended in the buffer.

◆ append() [2/2]

unsigned char& nkMemory::Buffer::append ( unsigned char  value)

Appends a byte to the buffer. This will cause the buffer to reallocate and copy data around. Use it wisely.

Parameters
valueThe value of the byte to append.
Returns
The reference over the element appended in the buffer.

◆ relinquishDataOwnership()

BufferInfo nkMemory::Buffer::relinquishDataOwnership ( )

Requests a buffer to abandon its ownership, leaving the management to the client using this function.

Returns
The information about the memory that was held by the buffer.
Remarks
Data has to be freed the way it has been allocated. Currently, memory is allocated using the default new[] allocator.

◆ operator[]() [1/2]

unsigned char& nkMemory::Buffer::operator[] ( unsigned long long  index)

Indexing operator.

Parameters
indexThe index of the element to index in the memory.
Returns
A reference over requested element.

◆ operator[]() [2/2]

const unsigned char& nkMemory::Buffer::operator[] ( unsigned long long  index) const

Indexing operator, const versioned.

Parameters
indexThe index of the element to index in the memory.
Returns
A reference over requested element.

◆ operator=() [1/2]

Buffer& nkMemory::Buffer::operator= ( const Buffer other)

Copy assignment operator.

Parameters
otherThe buffer to copy.
Returns
A reference over calling buffer, once updated.

◆ operator=() [2/2]

Buffer& nkMemory::Buffer::operator= ( Buffer &&  other)

Move assignment operator.

Parameters
otherThe buffer to move.
Returns
A reference over calling buffer, once updated.

◆ createAndTakeMemory()

static Buffer nkMemory::Buffer::createAndTakeMemory ( unsigned char *  data,
unsigned long long  size 
)
static

Static creation function for which the buffer takes ownership of the memory provided. This means the buffer will free the memory given once it is destroyed (through the operator delete[]).

Parameters
dataThe data the buffer will free.
sizeThe size, in bytes, of the data provided.
Remarks
This function will cause the memory to cross the dll-boundary (created by client code, deleted by dll code). As such, it should be used with care and is intended to be used in edge cases mostly. Prefer to create a BufferView over the memory region and manage its lifetime in the client code, if possible.

The documentation for this class was generated from the following file: